package com.duobaoyu.dby.supply.biz.converter;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.duobaoyu.dby.common.core.constant.CommonConstants;
import com.duobaoyu.dby.common.core.constant.StringPool;
import com.duobaoyu.dby.common.core.convert.bean.ReferenceMapper;
import com.duobaoyu.dby.supply.common.biz.bo.config.OperationLogBO;
import com.duobaoyu.dby.supply.db.entity.config.OperationLog;
import org.apache.commons.collections4.CollectionUtils;
import org.mapstruct.Builder;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import java.util.List;

/**
 * 操作日志Converter
 *
 * @author chenms
 * @date 2023/10/23 9:59 AM
 */
@Mapper(uses = ReferenceMapper.class, builder = @Builder(disableBuilder = true), componentModel = CommonConstants.COMPONENT_MODEL)
public interface OperationLogConverter {

    @Mapping(target = "content", expression = "java(getChangeDesc(operationLog))")
    OperationLogBO toOperationLog(OperationLog operationLog);

    List<OperationLogBO> toOperationLogList(List<OperationLog> logList);

    Page<OperationLogBO> toOperationLogPage(Page<OperationLog> logPage);

    default String getChangeDesc(OperationLog operationLog) {
        if (CollectionUtils.isNotEmpty(operationLog.getChangeItemList())) {
            StringBuilder stringBuilder = new StringBuilder();
            for (OperationLog.ChangeItem changeItem : operationLog.getChangeItemList()) {
                stringBuilder.append(changeItem.getField()).append("[").append(changeItem.getOldValue()).append("->").append(changeItem.getNewValue()).append("]").append(StringPool.SEMICOLON);
            }
            if (stringBuilder.length() > 0) {
                stringBuilder.setLength(stringBuilder.length() - 1);
            }
            return stringBuilder.toString();
        }
        return StringPool.EMPTY;
    }

}
